'''
A convenient way to load data for testing.  

Grabs remote data if it isn't locally cached.
'''
import os
from urllib import urlretrieve
from urllib2 import urlopen, HTTPError

def load_dataset(loc,tmp=os.curdir+'/tmp/'):
    '''
    Gets remote data, creates a tmp directory to cache the file 
    locally if it's not already there and returns the path to
    this file.

    Parameters
    ----------
    loc: string
    String of url for remote file.

    tmp: string
    Where to cache file locally.
    The default is './tmp/remotefile.name'.

    Returns
    -------
    A string of the path to local file.

    Notes
    -----
    Simply uses urllib.urlretrieve but tests if tmp data directory
    exists and creates it if it doesn't.

    Only tested for 'http://path/to/file.csv'
    '''
# Need to check if remote file exists
#    try:
#        remote=urlopen(loc)
#    except HTTPError, e:
#        print e.code+" Error at "+e.url
    if not os.path.exists(tmp):
        os.mkdir(tmp)
        print '''
Note that a temp directory has been created in
%s  
Cleanup of this directory should be done manually.
''' % os.path.abspath(tmp)

    return urlretrieve(loc,tmp+os.path.basename(loc))[0]
    

